﻿namespace ASB.CC.Web.EZVisit.View
{
    #region Using Directives

    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using ASBCC.Database.Service;
    using ASBCC.Workflow.Client;
    using BEGDC.CC.Web.Control;
    using BEGDC.CC.Web.Workflow;
    using BEGDC.Utility.Extensions;

    #endregion

    public partial class ApprovedList : BasePage
    {
        private string Sort
        {
            get
            {
                return Convert.ToString(this.ViewState["Sorting"]);
            }
            set
            {
                this.ViewState["Sorting"] = value;
            }
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.BindData(1);
            }
        }

        private void BindData(int pageIndex)
        {
            DataTable dtTasks = EZVisitApplicationManager.SelectByCondition(string.Format("IsEnabled = 1 and LGDDate is not null and ProcessStatus <> '{0}' and (readers like '%;{1};%' or requesterid = '{1}')", ProcessStatusConsts.End, this.UserInfo.EmployeeID)).Tables[0];

            if (this.Sort.IsNotNullOrWhiteSpace())
            {
                dtTasks.DefaultView.Sort = this.Sort;
            }
            int count = dtTasks.Rows.Count;

            this.dgTaskList.DataSource = dtTasks;
            this.dgTaskList.DataBind();

            this.SetPageCount(count, pageIndex);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            this.dgTaskList.AllowPaging = false;
            this.BindData(1);
            ExportDataGridToExcel.Export(this.Page, this.dgTaskList, "EZVisitApprovedRequests.xls");
        }

        public void dgTaskList_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (this.Sort.IndexOf(e.SortExpression) > -1 && this.Sort == e.SortExpression)
            {
                this.Sort = e.SortExpression + " desc";
            }
            else
            {
                this.Sort = e.SortExpression;
            }
            this.BindData(this._CurrentPageIndex);
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///   Required method for Designer support - do not modify
        ///   the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnPageFirst.Click += this.btnPageFirst_Click;
            this.btnPagePrev.Click += this.btnPagePrev_Click;
            this.btnPageNext.Click += this.btnPageNext_Click;
            this.btnPageLast.Click += this.btnPageLast_Click;
            this.btnGotoPage.Click += this.btnGotoPage_Click;
            this.dgTaskList.SortCommand += this.dgTaskList_SortCommand;
            this.btnExport.Click += this.btnExport_Click;
            this.Load += this.Page_Load;
        }

        #endregion

        #region Paging Events

        private void btnPageFirst_Click(object sender, ImageClickEventArgs e)
        {
            this.dgTaskList.CurrentPageIndex = 0;
            this.BindData(1);
        }

        private void btnPagePrev_Click(object sender, ImageClickEventArgs e)
        {
            if (this._CurrentPageIndex == 1)
            {
                return;
            }
            this.dgTaskList.CurrentPageIndex = this._CurrentPageIndex - 2;
            this.BindData(this._CurrentPageIndex - 1);
        }

        private void btnPageNext_Click(object sender, ImageClickEventArgs e)
        {
            if (this._CurrentPageIndex == this._PageCount)
            {
                return;
            }
            this.dgTaskList.CurrentPageIndex = this._CurrentPageIndex;
            this.BindData(this._CurrentPageIndex + 1);
        }

        private void btnPageLast_Click(object sender, ImageClickEventArgs e)
        {
            this.dgTaskList.CurrentPageIndex = this._PageCount - 1;
            this.BindData(this._PageCount);
        }

        private void btnGotoPage_Click(object sender, ImageClickEventArgs e)
        {
            if (this.txtPage.Text == string.Empty)
            {
                this.txtPage.Text = "1";
            }
            int gotopage = 1;
            try
            {
                gotopage = Convert.ToInt32(this.txtPage.Text);
            }
            catch
            {
            }
            int currentpage = 0;

            if (gotopage >= this._PageCount)
            {
                gotopage = this._PageCount;
                currentpage = gotopage - 1;
            }
            else if (gotopage <= 0)
            {
                gotopage = 1;
            }
            else
            {
                currentpage = gotopage - 1;
            }

            this.dgTaskList.CurrentPageIndex = currentpage;

            this.BindData(gotopage);

            this.txtPage.Text = string.Empty;
        }

        #endregion

        #region Paging Properties

        private int _PageCount
        {
            get
            {
                return Convert.ToInt32(this.ViewState["PageCount"]);
            }
            set
            {
                this.ViewState["PageCount"] = value;
            }
        }

        private int _CurrentPageIndex
        {
            get
            {
                return Convert.ToInt32(this.ViewState["CurrentPageIndex"]);
            }
            set
            {
                this.ViewState["CurrentPageIndex"] = value;
            }
        }

        private int _RowCount
        {
            get
            {
                return Convert.ToInt32(this.ViewState["RowCount"]);
            }
            set
            {
                this.ViewState["RowCount"] = value;
            }
        }

        private void SetPageNavi(int newPage)
        {
            if (newPage <= 1)
            {
                this.btnPageFirst.ImageUrl = "~/images/pg_first_inact.gif";
                this.btnPagePrev.ImageUrl = "~/images/pg_pre_inact.gif";
                this.btnPageFirst.Attributes["disabled"] = "true";
                this.btnPagePrev.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageFirst.ImageUrl = "~/images/pg_first.gif";
                this.btnPagePrev.ImageUrl = "~/images/pg_pre.gif";
                this.btnPageFirst.Attributes.Remove("disabled");
                this.btnPagePrev.Attributes.Remove("disabled");
            }

            if (newPage >= this._PageCount)
            {
                this.btnPageNext.ImageUrl = "~/images/pg_next_inact.gif";
                this.btnPageLast.ImageUrl = "~/images/pg_last_inact.gif";
                this.btnPageNext.Attributes["disabled"] = "true";
                this.btnPageLast.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageNext.ImageUrl = "~/images/pg_next.gif";
                this.btnPageLast.ImageUrl = "~/images/pg_last.gif";
                this.btnPageNext.Attributes.Remove("disabled");
                this.btnPageLast.Attributes.Remove("disabled");
            }

            this.lblCurrentPageIndex.Text = newPage.ToString();
            this.lblPageCount.Text = this._PageCount.ToString();
            this.lblRecordCount.Text = this._RowCount.ToString();
        }

        private void SetPageCount(int count, int pageIndex)
        {
            if (count > 0)
            {
                this._RowCount = count;
                this._PageCount = count/this.dgTaskList.PageSize;
                if (count%this.dgTaskList.PageSize > 0)
                {
                    this._PageCount++;
                }

                this._CurrentPageIndex = pageIndex;
                this.SetPageNavi(pageIndex);
            }
            else
            {
                this._RowCount = count;
                this._PageCount = count;
                this._CurrentPageIndex = 0;
                this.SetPageNavi(0);
            }
        }

        #endregion
    }
}